package one.stand.task;

import com.jmp.base.dto.OrderTimeoutDto;
import com.jmp.feign.OrderFeign;
import lombok.extern.slf4j.Slf4j;
import one.stand.common.cache.TicketOrderSumCacheModel;
import one.stand.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private RedisUtil redisUtil;

    @Scheduled(cron = "0 */5 * * * ?")
    public void updateRecordCount() {
        orderFeign.timeout(OrderTimeoutDto.builder().build());
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void updateTicketOrderSum() {
        TicketOrderSumCacheModel ticketOrderSumCacheModel = new TicketOrderSumCacheModel(redisUtil);
        Integer sum = ticketOrderSumCacheModel.get();
        if (Objects.nonNull(sum)) {
            log.info("ticket order sum:{}", sum);
            ticketOrderSumCacheModel.save(sum);
        } else {
            ticketOrderSumCacheModel.save(100000000);
        }
    }

}
